{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tune Visualization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to visualize results, please install `plotly` with the following command:\n",
    "\n",
    "  `pip install plotly`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "from ray.tune.visual_utils import load_results_to_df, generate_plotly_dim_dict\n",
    "import plotly\n",
    "import plotly.graph_objs as go\n",
    "plotly.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Specify the directory where all your results are in the variable `RESULTS_DIR`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "RESULTS_DIR = os.path.expanduser(\"~/ray_results\")\n",
    "df = load_results_to_df(RESULTS_DIR)\n",
    "[key for key in df]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Choose the fields you wish to visualize over in `GOOD_FIELDS`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "GOOD_FIELDS = ['experiment_id',\n",
    "               'num_sgd_iter',\n",
    "               'timesteps_total',\n",
    "               'episode_len_mean',\n",
    "               'episode_reward_mean']\n",
    "\n",
    "visualization_df = df[GOOD_FIELDS]\n",
    "visualization_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Enjoy.\n",
    "\n",
    "Documentation for this Plotly visualization can be found here: https://plot.ly/python/parallel-coordinates-plot/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = [go.Parcoords(\n",
    "            line = dict(color = 'blue'),\n",
    "            dimensions = [generate_plotly_dim_dict(visualization_df, field) \n",
    "                              for field in visualization_df])\n",
    "]\n",
    "\n",
    "plotly.offline.iplot(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
